﻿Imports System.Text
Imports System.Security.Cryptography

Public Class Utente

    Public Property PK_User As Integer

    Public Property FK_Anagrafica As Anagrafica

    Public Property username As String

    Public Property password As String

    Public intRuolo As Integer

    Public Enum Ruolo
        Admin = 1
        Other
    End Enum

    Public Shared Function md5(sPassword As String) As String
        Dim x As System.Security.Cryptography.MD5CryptoServiceProvider = New System.Security.Cryptography.MD5CryptoServiceProvider()
        Dim bs As Byte() = System.Text.Encoding.UTF8.GetBytes(sPassword)
        bs = x.ComputeHash(bs)
        Dim s As System.Text.StringBuilder = New System.Text.StringBuilder()
        For Each b As Byte In bs

            s.Append(b.ToString("x2").ToLower())
        Next
        Return s.ToString()
    End Function


    Public Shared Function LoadFromPk(objpk As Integer) As Utente
        Dim Usr As New Utente
        Dim sql As String = "SELECT * FROM Utente INNER JOIN Anagrafica ON FK_Anagrafica = PK_Anagrafica WHERE PK_Anagrafica = " & objpk
        Dim ds As New DataSet
        Dim dbO As New DBObject
        ds = dbO.FillDS(sql, Nothing, 1)
        If Not IsNothing(ds) AndAlso Not IsNothing(ds.Tables) AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
            Usr.PK_User = objpk
            Usr.username = ds.Tables(0).Rows(0).Item("username").ToString
            '  Usr.password = ds.Tables(0).Rows(0).Item("password").ToString
            Usr.FK_Anagrafica = New Anagrafica
            Usr.FK_Anagrafica.LoadFromPk(ds.Tables(0).Rows(0).Item("FK_Anagrafica").ToString)
            Return Usr
        End If
        Return Nothing
    End Function

End Class

